/***************************************************************************
*   Copyright (C) 2010-2011 by swkyer <swkyer@gmail.com>                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "stdafx.h"
#include "mips32opc.h"
#include "JlinkMIPS.h"
#include "Execution.h"


int mips32_exec_init(mips32_exec_context_t *pexec, void *pmips32)
{
    pexec->pmips32 = pmips32;

    return 0;
}

int mips32_exec_exit(mips32_exec_context_t *pexec)
{
    return 0;
}

ubase_t mips32_get_ecr(ejtag_t *pejtag)
{
    ubase_t ecr;

    ecr = MIPS32_ECR_CPU_RESET_OCCURED | MIPS32_ECR_ACCESS_PENDING |
            MIPS32_ECR_PROBE_ENABLE | MIPS32_ECR_PROBE_VECTOR;
    ejtag_instruction(pejtag, JTAG_REGISTER_CONTROL, NULL);
    ecr = ejtag_data(pejtag, ecr, NULL);

    return ecr;
}

int mips32_exec_microcode(mips32_exec_context_t *pexec, const ubase_t *pcodes)
{
    int retv;
    uint32_t i_cnt = 0;
    uint8_t bytev, *byteptr;
    uint16_t shortv, *shortptr;
    uint32_t *intptr;

    ubase_t ecr;
    ubase_t s_stack[8], s_stack_index;
    ubase_t s_result, s_psz;
    ubase_t s_address, s_data;
    ubase_t s_vram_acc_cnt;

    mips32_t *pmips32 = (mips32_t *)pexec->pmips32;
    s_stack_index = 0x00;
    s_vram_acc_cnt = 0;
    s_address = 0xFFFFFFFF;

    while (1)
    {
        byteptr = (uint8_t *)pexec->vram;
        shortptr = (uint16_t *)pexec->vram;

        if (ejtag_is_target_connect(pmips32->ejtag) == 0)
            return -1;

        ecr = mips32_get_ecr(pmips32->ejtag);

        s_psz = ecr & MIPS32_ECR_ACCESS_SIZE;
        if (!(ecr & MIPS32_ECR_ACCESS_PENDING))
        {
            // although we are in debug mode, but PrAcc is not pending, 
            // so we can not execute any instructions.
            // TODO, send message to console
            return -100;
        }
        i_cnt = 0;

        // read the pending address
        ejtag_instruction(pmips32->ejtag, JTAG_REGISTER_ADDRESS, NULL);
        ejtag_data(pmips32->ejtag, 0xFFFFFFFF, &s_address);

        // Check for read or write
        if (ecr & MIPS32_ECR_ACCESS_RW)
        {
            // read the pending data
            ejtag_instruction(pmips32->ejtag, JTAG_REGISTER_DATA, NULL);
            ejtag_data(pmips32->ejtag, 0xFFFFFFFF, &s_data);

            // clear PrAcc, let processor continues
            s_result = MIPS32_ECR_CPU_RESET_OCCURED | MIPS32_ECR_PROBE_ENABLE |
                        MIPS32_ECR_PROBE_VECTOR;
            ejtag_instruction(pmips32->ejtag, JTAG_REGISTER_CONTROL, NULL);
            ejtag_data(pmips32->ejtag, s_result, &s_address);

            // write the data to the location
            if (s_address == MIPS32_VIRTUAL_STACK_ADDR)
            {
                // the virtual stack
                s_stack[s_stack_index++] = s_data;
            }
            else if ((s_address >= MIPS32_VIRTUAL_RAM_START) 
                && (s_address < MIPS32_VIRTUAL_RAM_START + MIPS32_VIRTUAL_RAM_SIZE))
            {
                // byte lane
                if (s_psz == MIPS32_ECR_ACCESS_BYTE)
                {
                    bytev = s_address & 0x03;
                    bytev = (s_data & (0xFF<<(bytev*8))) >> (8*bytev);
                    byteptr[s_vram_acc_cnt++] = bytev;
                }
                else if (s_psz == MIPS32_ECR_ACCESS_HWORD)
                {
                    bytev = s_address & 0x01;
                    if (bytev == 0)
                        shortv = s_data & 0xFFFF;
                    else
                        shortv = (s_data & 0xFFFF0000) >> 16;
                    shortptr[s_vram_acc_cnt++] = shortv;
                }
                else if (s_psz == MIPS32_ECR_ACCESS_WORD)
                {
                    pexec->vram[s_vram_acc_cnt++] = s_data;
                }
            }
            else if ((s_address >= MIPS32_REGISTER_LOCATION) 
                && (s_address < MIPS32_REGISTER_LOCATION + sizeof(mips32_regs_t)))
            {
                intptr = (ubase_t *)&pmips32->regs;
                retv = (s_address - MIPS32_REGISTER_LOCATION) / sizeof(ubase_t);
                intptr[retv] = s_data;
            }
            else if ((s_address >= MIPS32_ARG0_ADDR) && (s_address <= MIPS32_ARG3_ADDR))
            {
                intptr = (ubase_t *)&pexec->reg_arg0;
                retv = (s_address - MIPS32_REGISTER_LOCATION) / sizeof(ubase_t);
                intptr[retv] = s_data;
            }
            else
            {
                // TODO, error handing
                return -1;
            }
        }
        else
        {
            // Processor is reading from us
            if (s_address == MIPS32_VIRTUAL_STACK_ADDR)
            {
                // virtual stack
                s_data = s_stack[--s_stack_index];
            }
            else if (s_address < MIPS32_DMSEG_RAM_BEGIN)
            {
                s_data = (s_address - MIPS32_DMSEG_BASEADDRESS) / sizeof(ubase_t);
                s_data = pcodes[s_data];
            }
            else if ((s_address >= MIPS32_VIRTUAL_RAM_START)
                && (s_address < MIPS32_VIRTUAL_RAM_START + MIPS32_VIRTUAL_RAM_SIZE))
            {
                if (s_psz == MIPS32_ECR_ACCESS_BYTE)
                {
                    s_data = byteptr[s_vram_acc_cnt++];
                }
                else if (s_psz == MIPS32_ECR_ACCESS_HWORD)
                {
                    s_data = shortptr[s_vram_acc_cnt++];
                }
                else if (s_psz == MIPS32_ECR_ACCESS_WORD)
                {
                    s_data = pexec->vram[s_vram_acc_cnt++];
                }
            }
            else if ((s_address >= MIPS32_REGISTER_LOCATION)
                && (s_address < MIPS32_REGISTER_LOCATION + sizeof(mips32_regs_t)))
            {
                intptr = (ubase_t *)&pmips32->regs;
                retv = (s_address - MIPS32_REGISTER_LOCATION) / sizeof(ubase_t);
                s_data = intptr[retv];
            }
            else if ((s_address >= MIPS32_ARG0_ADDR) && (s_address <= MIPS32_ARG3_ADDR))
            {
                intptr = (ubase_t *)&pexec->reg_arg0;
                retv = (s_address - MIPS32_REGISTER_LOCATION) / sizeof(ubase_t);
                s_data = intptr[retv];
            }
            else
            {
                // TODO, error handing
                return -1;
            }

            // feed the processor
            ejtag_instruction(pmips32->ejtag, JTAG_REGISTER_DATA, NULL);
            ejtag_data(pmips32->ejtag, s_data, &s_data);

            // clear PrAcc, let processor eat it
            s_result = MIPS32_ECR_CPU_RESET_OCCURED | MIPS32_ECR_PROBE_ENABLE |
                MIPS32_ECR_PROBE_VECTOR;
            ejtag_instruction(pmips32->ejtag, JTAG_REGISTER_CONTROL, NULL);
            ejtag_data(pmips32->ejtag, s_result, &s_result);
        }
    }

    return 0;
}
